package com.ptp.backendcertms.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ptp.backendcertms.api.external.dto.req.ReportDeleteReqDto;
import com.ptp.backendcertms.api.external.dto.req.ReportDetailReqDto;
import com.ptp.backendcertms.api.external.dto.req.ReportListReqDto;
import com.ptp.backendcertms.api.external.dto.req.ReportUpdateReqDto;
import com.ptp.backendcertms.api.external.dto.res.*;
import com.ptp.backendcertms.basiccompnents.data.dao.entity.ReportPo;
import com.ptp.backendcertms.basiccompnents.data.dao.service.IReportService;
import com.ptp.backendcertms.basiccompnents.properties.ProjectProperties;
import com.ptp.backendcertms.business.handler.PageInfoCommonHandler;
import com.ptp.backendcertms.business.service.IReportBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报告业务服务实现
 *
 * @author songcongcong
 * @date 2025/5/5
 */
@Service
public class ReportBusinessServiceImpl implements IReportBusinessService {
    @Resource
    private IReportService reportService;
    @Resource
    private PageInfoCommonHandler pageInfoCommonHandler;
    @Resource
    private ProjectProperties projectProperties;

    @Override
    public ReportListResDto getReportList(ReportListReqDto reqDto) {
        PageInfoDto pageInfoDto = pageInfoCommonHandler.buildPageInfo(0L, reqDto.getCurrent(), reqDto.getPageSize());
        ReportListResDto resDto = new ReportListResDto();
        resDto.setPageInfo(pageInfoDto);
        if (StringUtils.isNotBlank(reqDto.getOrganizationName()) && Arrays.stream(projectProperties.getTemplate().getOrgNameShield()).anyMatch(t -> reqDto.getOrganizationName().equals(t))) {
            return resDto;
        }
        ReportPo queryPo = new ReportPo();
        queryPo.setReportName(reqDto.getReportName());
        queryPo.setReportNo(reqDto.getReportNo());
        queryPo.setOrganizationName(reqDto.getOrganizationName());
        queryPo.setSocialCreditCode(reqDto.getSocialCreditCode());
        // 分页查询报告列表
        IPage<ReportPo> reportPoPage = reportService.pageListMc(queryPo, reqDto.getReleaseStartTime(), reqDto.getReleaseEndTime(), reqDto.getCurrent(), reqDto.getPageSize());
        resDto.getPageInfo().setTotal(reportPoPage.getTotal());
        // 封装证书列表
        List<ReportInfoResDto> list = reportPoPage.getRecords().stream().map(t -> {
            ReportInfoResDto reportInfoResDto = new ReportInfoResDto();
            // 流水号
            reportInfoResDto.setSerialNo(t.getSerialNo());
            // 组织名称
            reportInfoResDto.setOrganizationName(t.getOrganizationName());
            // 报告编号
            reportInfoResDto.setReportNo(t.getReportNo());
            // 报告名称
            reportInfoResDto.setReportName(t.getReportName());
            // 发布时间
            reportInfoResDto.setReleaseTime(t.getReleaseTime());

            // 统一社会信用代码
            reportInfoResDto.setSocialCreditCode(t.getSocialCreditCode());
            return reportInfoResDto;
        }).collect(Collectors.toList());
        resDto.setList(list);
        return resDto;
    }

    @Override
    public ReportDetailResDto getReportDetail(ReportDetailReqDto reqDto) {
        ReportPo reportPo = reportService.getBySerialNo(reqDto.getSerialNo());
        ReportDetailResDto resDto = new ReportDetailResDto();
        // 流水号
        resDto.setSerialNo(reportPo.getSerialNo());
        // 组织名称
        resDto.setOrganizationName(reportPo.getOrganizationName());
        // 报告编号
        resDto.setReportNo(reportPo.getReportNo());
        // 报告名称
        resDto.setReportName(reportPo.getReportName());
        // 发布时间
        resDto.setReleaseTime(reportPo.getReleaseTime());

        // 统一社会信用代码
        resDto.setSocialCreditCode(reportPo.getSocialCreditCode());
        return resDto;
    }

    @Override
    public void deleteReport(ReportDeleteReqDto reqDto) {
        ReportPo updatePo = new ReportPo();
        updatePo.setSerialNo(reqDto.getSerialNo());
        reportService.deleteBySerialNo(updatePo);
    }

    @Override
    public void updateReport(ReportUpdateReqDto reqDto) {
        ReportPo updatePo = new ReportPo();
        updatePo.setSerialNo(reqDto.getSerialNo());
        updatePo.setReportNo(reqDto.getReportNo());
        updatePo.setOrganizationName(reqDto.getOrganizationName());
        updatePo.setSocialCreditCode(reqDto.getSocialCreditCode());
        updatePo.setReportName(reqDto.getReportName());
        updatePo.setReleaseTime(reqDto.getReleaseTime());
        reportService.updateReportBySerialNo(updatePo);
    }
}
